package org.ax.service.sys.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.ax.mapper.sys.UserMapper;
import org.ax.pojo.sys.User;
import org.ax.service.sys.RoleService;
import org.ax.service.sys.UserRoleService;
import org.ax.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-23-14:49
 */
@Slf4j
@Service
public class IUserService extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleService userRoleService;
    @Override
    public User login(String userName) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        queryWrapper.or();
        queryWrapper.eq("email", userName);
        return  getBaseMapper().selectOne(queryWrapper);
    }

    @Override
    public Page<User> searchUserPage(Page page) {
        Page page1 = getBaseMapper().selectPage(page, null);
        List<User> userList=(List<User>)page1.getRecords();
        for (User user : userList) {
            user.setRole(roleService.toRoleId(userRoleService.toUserIdFindRole(user.getId())));
            log.debug("用户:{}",user.getRole().toString());
        }
        page1.setRecords(userList);
        return page1;
    }

    @Override
    public boolean addUser(User user) {
        return getBaseMapper().insert(user)>0;
    }

    @Override
    public boolean updateUser(User user) {
        return getBaseMapper().updateById(user)>0;
    }

    @Override
    public boolean deletesBatch(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public List<User> friendlyUser(String id) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        原符号 < <= > >= <>
//        对应函数 lt() le() gt() ge() ne()
        queryWrapper.ne("id",id); //表示不等于
        queryWrapper.orderByDesc("style");
        return getBaseMapper().selectList(queryWrapper);
    }
}
